#include "CSet.h"
bool Set::operator <=(const Set& s)const
{
	bool _Can_It_Find[1000] = { 0 };
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= s.n; j++)
		{
			if (this->pS[i] == s.pS[j])_Can_It_Find[i] = true;
		}
	}//便利所有，若A[i]被找到，则令bool[i]为1
	for (int i = 1; i <= n; i++)
	{
		if (_Can_It_Find[i] == 0)return false;

	}//有一个没找到就会返回FALSE
	return true;
}

Set& Set::operator +=(int e)
{
	bool _Is_There_Element_e = 0;
	for (int i = 1; i <= n; i++)
	{
		if (pS[i] == e)_Is_There_Element_e = 1;
	}//循环一下，看看有没有e，如果有就不做操作
	if (_Is_There_Element_e)return *this;

	int* tmp = new int[n + 2];
	for (int i = 1; i <= n; i++)
	{
		tmp[i] = this->pS[i];
	}//拷贝所有元素
	tmp[n + 1] = e;//把e拷贝到最后一个
	delete[]pS;
	pS = tmp;
	this->n = this->n + 1;
	return *this;
}

Set& Set::operator -=(int e)
{
	bool _Is_There_Element_e = 0;
	for (int i = 1; i <= n; i++)
	{
		if (pS[i] == e)_Is_There_Element_e = 1;
	}//循环一下，看看有没有e，如果没有就不做操作
	if (_Is_There_Element_e == 0)return *this;

	int* tmp = new int[n];
	for (int i = 1, j=1; i <= n; i++)
	{
		if (pS[i] == e)continue;
		tmp[j] = pS[i];
		j++;
	}//将非e的元素依次拷贝
	delete[]pS;
	pS = tmp;
	n = n - 1;
	return *this;
}

bool Set::operator ==(const Set& s)const
{
	//若数量相等,再为子集，则必然相等
	if (this->n != s.n)return false;
	else return (*this <= s);
	
}


Set Set::operator |(const Set& s)const
{
	bool _Can_It_Find[1000] = { 0 };
	int count = 0;

	for (int j = 1; j <= s.n; j++)
	{
		for (int i = 1; i <= this->n; i++)
		{
			if (s.pS[j] == this->pS[i])_Can_It_Find[j] = 1;//新的集合在旧的集合里面有

		}
		if (!_Can_It_Find[j])count++;//循环一圈仍然没有，就计数
	}

	int tmp_n = n + count;
	int* p = new int[tmp_n + 1];
	
	int k = 1;//tmp的计数
	for (int i = 1; i <= this->n; i++)
	{
		p[k++] = pS[i];//先把a照搬过来
	}
	
	for (int j = 1; j <= s.n; j++)
	{
		if (_Can_It_Find[j])continue;
		p[k++] = s.pS[j];
			//把b的不重复的搬运过来
	}
	Set tmp;
	tmp.n = tmp_n;
	tmp.pS = p;
	return tmp;
}


Set Set::operator &(const Set& s)const
{
	bool _Can_It_Find[1000] = { 0 };
	int count = 0;
	for (int j = 1; j <= s.n; j++)
	{
		for (int i = 1; i <= this->n; i++)
		{
			if (s.pS[j] == this->pS[i])_Can_It_Find[j] = 1;//新的集合在旧的集合里面有

		}
		if (_Can_It_Find[j])count++;//循环一圈仍然没有，就计数
	}
	Set tmp;
	tmp.n = count;
	tmp.pS = new int[count + 1];
	int k = 0;
	for (int j = 1; j <= s.n; j++)
	{
		if (_Can_It_Find[j])tmp.pS[++k] = s.pS[j];
	}
	return tmp;
}

Set Set::operator -(const Set& s)const
{

	bool _Can_It_Find[1000] = { 0 };
	int count = 0;
	for (int i = 1; i <= this->n; i++)
	{
		for (int j = 1; j <= s.n; j++)
		{
			if (s.pS[j] == this->pS[i])_Can_It_Find[i] = 1;//新的集合在旧的集合里面有

		}
		if (!_Can_It_Find[i])count++;//循环一圈仍然没有，就计数
	}
	Set tmp;
	tmp.n = count;
	tmp.pS = new int[count+1];
	int k = 0;
	for (int i = 1; i <= this->n; i++)
	{
		if (!_Can_It_Find[i])tmp.pS[++k] = this->pS[i];
	}
	return tmp;
}
